'''Module of useful utility functions'''


## Utility #######################################
class MaybeObj(object):
    '''Proxy object to harmlessly do nothing in the case that an object instance is None'''
    
    def __init__(self, obj):
        self.obj = obj

    def __getattr__(self, name):
        if self.obj is not None:
            return getattr(self.obj, name)
        else:
            class __anon__(object):
                def __init__(self):
                    super(__anon__, self).__init__()
                    
                def __call__(self, *args):
                    '''Harmlessly do nothing'''
                    pass
                
            return __anon__()

    def valid_obj(self):
        return self.obj is not None


class Counter(object):    
    def __init__(self, initial_value=0):
        self.id = initial_value

    def next_count(self):
        tmp = self.id
        self.id += 1
        return tmp

    def __call__(self):
        return self.next_count()

class UniqueID(Counter):
    def next_id(self):
        return self.next_count()

    def __call__(self):
        return self.next_id()


class Cycle(object):
    def __init__(self, lst):
        self.lst = list(lst)
        self.index = 0

    def get_next(self):
        obj = self.lst[ self.index ]
        self.index = (self.index + 1) % len(self.lst)
        return obj

def update_to_dataflow(f):
    '''
    Turns a stateful list operation into one that returns a modified copy
    '''

    def __anon__(seq, *args, **kwargs):
        cpy = list(seq)
        f(cpy, *args, **kwargs)
        return cpy
    
    return __anon__

if __name__ == '__main__':
    pass
